package com.xintoucloud.springbootguide.controller;

import com.xintoucloud.springbootguide.entity.Student;
import com.xintoucloud.springbootguide.model.ResponseResult;
import com.xintoucloud.springbootguide.service.ClazzService;
import com.xintoucloud.springbootguide.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@CrossOrigin({"http://localhost:63342"})
@RestController
public class StudentController {
	
	@Autowired
	StudentService studentService;
	@Autowired
	ClazzService clazzService;

	//object->json 序列化
	//json->object 反序列化

	@PostMapping("/student")
	public ResponseResult add(@RequestBody Student student) {
		System.out.println(student);
		studentService.saveStudent(student);
		return ResponseResult.success();
	}
	@DeleteMapping("/student/{sid}")
	public ResponseResult delete(@PathVariable("sid") Integer sid) {
		studentService.deleteStudent(sid);
		return ResponseResult.success();
	}

	@PutMapping("/student")
	public ResponseResult update(@RequestBody Student student) {
		System.out.println(student);
		studentService.update(student);
		return ResponseResult.success();
	}

	@GetMapping("/student")
	public ResponseResult<Student> list(int page,int limit) {
		return studentService.listStudent(page,limit);
	}
	/*@GetMapping("/student/{sid}")
	public String toUpdatePage(@PathVariable("sid") Integer sid, Model m) {
		m.addAttribute("clazzes", clazzService.listClazz());
		m.addAttribute("student", studentService.getStudent(sid));
		return "student/update";
	}*/

}
